package org.csu.laboratoryequipment.controller;

import org.csu.laboratoryequipment.common.CommonResponse;
import org.csu.laboratoryequipment.entity.Borrow;
import org.csu.laboratoryequipment.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/")
public class BorrowController {

    @Autowired
    private BorrowService borrowService;

    @GetMapping("borrows")
    public CommonResponse<List<Borrow>> Get(){
        return borrowService.getBorrowList();
    }

    @GetMapping("borrows/{id}")
    public CommonResponse<List<Borrow>> Get(@PathVariable("id") int transactionid){
        return borrowService.getBorrowById(transactionid);
    }

    @GetMapping("borrows/users/{id}")
    public CommonResponse<List<Borrow>> Get(@PathVariable("id") String userid){
        return borrowService.getBorrowByUserId(userid);
    }


    @DeleteMapping("borrows/{id}")
    public CommonResponse<Borrow> DELETE(@PathVariable("id") int transactionid){
        return borrowService.deleteBorrowById(transactionid);
    }
    @PostMapping("borrows")
    public CommonResponse<Borrow> POST(@RequestParam("id") String userid,
                                             @RequestParam("equipmentid")  String equipmentid,
                                             @RequestParam("borrowdate") Timestamp borrowdate){
        return borrowService.postBorrow(userid,equipmentid,borrowdate);
    }

    @PutMapping("borrows")
    public CommonResponse<Borrow> PUT(@RequestParam("id") int transactionid,
                                            @RequestParam("returndate") Timestamp returndate){
        return borrowService.putBorrow(transactionid,returndate);
    }

}
